#include <QtGui>

#include "operationDialog.h"
#include "operationPages.h"
#include "../src/data.h"


OperationDialog::OperationDialog(QWidget *_parent, std::vector<QImage *> _i, std::vector<QString> _names) : QDialog(_parent), originalImages(_i){
    for(int i = 0; i < _i.size(); i++)
        qDebug() << i <<": " <<_i.at(i)->format();
    testImage = NULL;
//    testImage = new QImage(*(_i.at(0)));

//    filter = new SpatialFilter(new Data(testImage->bits(), testImage->width(), testImage->height()));



    QSplitter *horizontalSplitter = new QSplitter(this);
    horizontalSplitter->setOrientation(Qt::Horizontal);
    QSplitter *verticalSplitter = new QSplitter(horizontalSplitter);
    verticalSplitter->setOrientation(Qt::Vertical);


    preview = new Image(verticalSplitter, testImage);

    contentsWidget = new QListWidget(horizontalSplitter);
    contentsWidget->setResizeMode(QListView::Adjust);
    contentsWidget->setMinimumWidth(200);
    contentsWidget->setMaximumWidth(300);
    contentsWidget->setSpacing(7);



    /*INCLUINDO AS PAGINAS*/
    pagesWidget = new QStackedWidget(verticalSplitter);

    OperationPage *op = new OperationPage(_names, pagesWidget);
    connect(op, SIGNAL(done()), this, SLOT(tryOperation()));
    pagesWidget->addWidget(op);

    QPushButton *closeButton = new QPushButton(tr("Fechar"));
    QPushButton *applyButton = new QPushButton(tr("Aplicar"));

    connect(closeButton, SIGNAL(clicked()), this, SLOT(notApply()));
    connect(applyButton, SIGNAL(clicked()), this, SLOT(apply()));


    createIcons();
    contentsWidget->setCurrentRow(0);

    verticalSplitter->addWidget(preview);
    verticalSplitter->addWidget(pagesWidget);
    horizontalSplitter->addWidget(contentsWidget);
    horizontalSplitter->addWidget(verticalSplitter);

    resize(600, 400);

    QHBoxLayout *buttonsLayout = new QHBoxLayout;
    buttonsLayout->addStretch(1);
    buttonsLayout->addWidget(applyButton);
    buttonsLayout->addWidget(closeButton);


    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(horizontalSplitter);
    mainLayout->addLayout(buttonsLayout);
    setLayout(mainLayout);

    setWindowTitle(tr("Operações binarias"));
}

OperationDialog::~OperationDialog(){
}

void OperationDialog::createIcons(){
    addButton(new QListWidgetItem(contentsWidget), "images/sem_ico.png", QString::fromUtf8(ADDITION));
    addButton(new QListWidgetItem(contentsWidget), "images/sem_ico.png", QString::fromUtf8(SUBTRACTION));
    addButton(new QListWidgetItem(contentsWidget), "images/sem_ico.png", QString::fromUtf8(MULTIPLICATION));
    addButton(new QListWidgetItem(contentsWidget), "images/sem_ico.png", QString::fromUtf8(DIVISION));

    connect(contentsWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
        this, SLOT(changePage(QListWidgetItem*,QListWidgetItem*)));
}

void OperationDialog::addButton(QListWidgetItem *_button, QString _ico, QString _name){
//    _button->setIcon(QIcon(_ico));
    _button->setText(_name);
    _button->setTextAlignment(Qt::AlignLeft | Qt::AlignBottom);
    _button->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}

void OperationDialog::apply(){
//    if(originalImage) delete originalImage;

    emit done(new QImage(*testImage));
    emit done();
    accept();
}

void OperationDialog::notApply(){
    emit cancel();
    close();
}

void OperationDialog::changePage(QListWidgetItem *current, QListWidgetItem *previous){
    if (!current)
        current = previous;

    pagesWidget->setCurrentIndex(contentsWidget->row(current));
    tryOperation();
}

void OperationDialog::tryOperation(){
    QListWidgetItem *current = contentsWidget->currentItem();

    OperationPage *op = (OperationPage *)pagesWidget->currentWidget();
    QImage *i1 = originalImages.at(op->getImage1());
    QImage *i2 = originalImages.at(op->getImage2());
    Data *d = NULL;
    Data d1(i1->bits(), i1->width(), i1->height());
    Data d2(i2->bits(), i2->width(), i2->height());

    if(current->text() == QString::fromUtf8(ADDITION)){
        d = d1 + d2;
    }else if(current->text() == QString::fromUtf8(SUBTRACTION)){
        d = d1 - d2;
    }else if(current->text() == QString::fromUtf8(MULTIPLICATION)){
        d = d1 * d2;
    }else if(current->text() == QString::fromUtf8(DIVISION)){
        d = d1 / d2;
    }
    if(testImage) delete testImage;
    testImage = new QImage(d->getData(), d->getWidth(), d->getHeight(), d->getWidth()*4, i1->format());

//    pagesWidget->show();

    update();
}

void OperationDialog::update(){
    preview->update(testImage);
}

//void OperationDialog::revert(){
//    delete testImage;
//    testImage = new QImage(*originalImage);
//    filter->getData()->setAll(testImage->bits(), testImage->width(), testImage->height());
//}

void OperationDialog::changeFilter(QString _filter){
    for(int i = 0; i < contentsWidget->count(); i++){
        if(contentsWidget->item(i)->text() == _filter){
            contentsWidget->setCurrentRow(i);
            return;
        }
    }
}
